package com.blb.dao.admin.impl;

import com.blb.dao.admin.BrandDao;
import com.blb.pojo.Brand;
import com.blb.pojo.ProductType;
import com.blb.utils.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class BrandDaoImpl implements BrandDao {

    @Override
    public Brand selectByName(String name) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        Brand brand = JDBCUtils.getOne(connection, Brand.class, "select * from t_brand where brand_name = ?", name);
        connection.close();
        return brand;
    }

    @Override
    public List<Brand> selectPage(int page, int size) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select tb.*,tp.name  from t_brand tb left join t_product_type tp on tp.id = tb.product_type_id limit ?,?";
        List<Brand> list = JDBCUtils.getList(connection, Brand.class, sql, (page - 1) * size, size);
        connection.close();
        return list;
    }

    @Override
    public int count() throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(id) count from t_brand ";

        ResultSet resultSet = JDBCUtils.prepareStatement(connection, sql).executeQuery();
        resultSet.next();
        int count = resultSet.getInt("count");
        connection.close();
        return count;
    }

    @Override
    public void insert(Brand brand) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "insert into t_brand(id,brand_name,brand_img,product_type_id) values(?,?,?,?)",
                brand.getId(), brand.getBrandName(), brand.getBrandImg(), brand.getProductTypeId()).executeUpdate();
        connection.close();
    }

    @Override
    public void deleteById(int id) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "delete from t_brand where id = ?", id).executeUpdate();
        connection.close();
    }

    @Override
    public void batchDelete(String[] ids) throws SQLException, ClassNotFoundException {
        String sql = "delete from t_brand where id in (";

        for (int i = 0; i < ids.length; i++) {
            sql += "?";
            if (i != ids.length - 1) {
                sql += ",";
            }
        }
        sql += ")";

        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, sql, ids).executeUpdate();
        connection.close();
    }

    @Override
    public Brand selectById(int id) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        Brand brand = JDBCUtils.getOne(connection, Brand.class, "select tb.*,tp.name from t_brand tb left join t_product_type tp on tp.id = tb.product_type_id where tb.id = ?", id);
        connection.close();
        List<ProductType> list = getPType();
        brand.setList(list);
        return brand;
    }

    @Override
    public void update(Brand brand) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "update t_brand set brand_name=?,brand_img=?,product_type_id=? where id=?"
                , brand.getBrandName(), brand.getBrandImg(), brand.getProductTypeId(), brand.getId()).executeUpdate();
        connection.close();
    }

    @Override
    public List<ProductType> getPType() throws Exception {
        ArrayList<ProductType> list = new ArrayList<>();
        ProductType type;
        Connection connection = JDBCUtils.getConnection();
        ResultSet resultSet = JDBCUtils.prepareStatement(connection, "select id,name from t_product_type").executeQuery();
        while (resultSet.next()) {
            type = new ProductType();
            type.setId(resultSet.getInt("id"));
            type.setName(resultSet.getString("name"));
            list.add(type);
        }
        connection.close();
        return list;
    }

    @Override
    public List<Brand> selectByTypeId(int productTypeId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        List<Brand> list = JDBCUtils.getList(connection, Brand.class, "select * from t_brand where product_type_id = ?", productTypeId);
        connection.close();
        return list;
    }
}
